package com.heakey.common.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.*;
import lombok.extern.slf4j.Slf4j;

import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 * @Description http请求工具类
 * @Author Bing
 * @Date 2024/10/18 11:30
 */
@Slf4j
public class HttpUtils {

    private final static HttpConfig DEFAULT_CONFIG = new HttpConfig();

    private HttpConfig httpConfig;

    private HttpUtils(HttpConfig httpConfig){
        this.httpConfig = httpConfig;
    }

    public static HttpUtils create(){
        return new HttpUtils(DEFAULT_CONFIG);
    }

    public static HttpUtils create(Supplier<HttpConfig> httpConfig){
        return new HttpUtils(httpConfig.get());
    }

    /**
     * get请求
     * @param url
     * @param jsonParams
     * @return
     */
    public String get(String url, String jsonParams){
        return getResult(url, jsonParams,httpConfig,httpRequest -> {
            httpRequest.header("Content-Type", ContentType.MULTIPART.getValue());
            httpRequest.setMethod(Method.GET);
        });
    }

    /**
     * get请求
     * @param url
     * @param jsonParams
     * @return
     */
    public String get(String url, String jsonParams,Consumer<HttpRequest> supplier){
        return getResult(url, jsonParams,httpConfig,httpRequest -> {
            httpRequest.header("Content-Type", ContentType.MULTIPART.getValue());
            httpRequest.setMethod(Method.GET);
            supplier.accept(httpRequest);
        });
    }

    /**
     * postJson请求
     * @param url
     * @param jsonParams
     * @return
     */
    public String postJson(String url, String jsonParams){
        return postJson(url, jsonParams, httpConfig);
    }

    /**
     * postForm请求
     * @param url
     * @param jsonParams
     * @return
     */
    public String postForm(String url, String jsonParams){
        return postForm(url, jsonParams, httpConfig);
    }

    /**
     * postForm请求
     * @param url
     * @param jsonParams
     * @param httpConfig
     * @return
     */
    private String postForm(String url, String jsonParams, HttpConfig httpConfig){
        return getResult(url, jsonParams,httpConfig,httpRequest -> {
            httpRequest.header("Content-Type", ContentType.MULTIPART.getValue());
            httpRequest.setMethod(Method.POST);
        });
    }

    /**
     * 底层请求方法
     * @param url
     * @param jsonParams
     * @param httpConfig
     * @return
     */
    private String postJson(String url, String jsonParams, HttpConfig httpConfig){
        return getResult(url, jsonParams,httpConfig,httpRequest -> {
            httpRequest.header("Content-Type", ContentType.JSON.getValue());
            httpRequest.setMethod(Method.POST);
        });
    }

    /**
     * 封装请求参数
     * @param url
     * @param jsonParams
     * @param consumer
     * @return
     */
    private String getResult(String url, String jsonParams,HttpConfig httpConfig, Consumer<HttpRequest> consumer) {
        // 创建一个POST请求
        HttpResponse httpResult = null;
        try {
            HttpRequest httpRequest = HttpRequest.of(url);
            httpRequest.setConfig(httpConfig);
            if(ObjectUtil.isNotNull(consumer)){
                consumer.accept(httpRequest);
            }
            httpResult = httpRequest.body(jsonParams).execute();
            if (httpResult.getStatus() != 200) {
                return null;
            }
        } catch (Exception e) {
            log.error("请求失败", e);
        } finally {
            if (httpResult != null) {
                httpResult.close();
            }
        }
        String result = httpResult.body();
        log.info("======> HttpUtils 请求 url:{} | param:{} | 结果：{}",url,jsonParams,StrUtil.sub(result,0,5000));
        return result;
    }
}